package cn.edu.ldu.views.operator;

import cn.edu.ldu.controller.UserController;
import cn.edu.ldu.entity.FriendsEntity;
import cn.edu.ldu.entity.UserAccountEntity;
import cn.edu.ldu.protocol.Protocol;
import cn.edu.ldu.util.Consts;
import cn.edu.ldu.util.L;
import cn.edu.ldu.util.Translate;
import com.alibaba.fastjson.JSON;

import javax.swing.*;
import java.io.IOException;
import java.net.*;

/**
 *
 * @author DYZ
 */
public class Friends extends javax.swing.JFrame {

    /**
     * Creates new form FriendsUI
     */
    public Friends(FriendsEntity friends) {
        friend = friends;
        initComponents(friends);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents(FriendsEntity friend) {

        jPanel1 = new javax.swing.JPanel();
        btnReset = new javax.swing.JButton();
        btnEdit = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        txtUserId = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        txtUsername = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        txtUserRename = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        txtStyle = new javax.swing.JTextArea();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        btnReset.setText("重置");
        btnReset.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnResetMouseClicked(evt);
            }
        });

        btnEdit.setText("修改");
        btnEdit.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnEditMouseClicked(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(77, 77, 77)
                                .addComponent(btnReset, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 159, Short.MAX_VALUE)
                                .addComponent(btnEdit)
                                .addGap(90, 90, 90))
        );
        jPanel1Layout.setVerticalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(29, 29, 29)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(btnReset)
                                        .addComponent(btnEdit))
                                .addContainerGap(44, Short.MAX_VALUE))
        );

        getContentPane().add(jPanel1, java.awt.BorderLayout.PAGE_END);

        jLabel1.setText("好友账户：");

        txtUserId.setEditable(false);
        txtUserId.setText(friend.getUserId().toString());

        jLabel2.setText("好友昵称：");

        txtUsername.setEditable(false);
        txtUsername.setText(friend.getUserAccountEntity().getRealName());

        jLabel3.setText("好友备注：");

        txtUserRename.setText(friend.getFriendRemark());

        jLabel4.setText("好友个性签名：");
        txtStyle.setText(friend.getUserAccountEntity().getSignature());
        txtStyle.setEditable(false);
        txtStyle.setColumns(20);
        txtStyle.setRows(5);
        jScrollPane1.setViewportView(txtStyle);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
                jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel2Layout.createSequentialGroup()
                                .addGap(51, 51, 51)
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                .addComponent(jLabel1)
                                                .addComponent(jLabel2)
                                                .addComponent(jLabel3))
                                        .addComponent(jLabel4))
                                .addGap(49, 49, 49)
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 190, Short.MAX_VALUE)
                                        .addComponent(txtUserRename)
                                        .addComponent(txtUsername)
                                        .addComponent(txtUserId))
                                .addGap(82, 82, 82))
        );
        jPanel2Layout.setVerticalGroup(
                jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel2Layout.createSequentialGroup()
                                .addGap(27, 27, 27)
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(jLabel1)
                                        .addComponent(txtUserId, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(28, 28, 28)
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(jLabel2)
                                        .addComponent(txtUsername, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(23, 23, 23)
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(jLabel3)
                                        .addComponent(txtUserRename, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(jPanel2Layout.createSequentialGroup()
                                                .addGap(29, 29, 29)
                                                .addComponent(jLabel4))
                                        .addGroup(jPanel2Layout.createSequentialGroup()
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                .addContainerGap(18, Short.MAX_VALUE))
        );

        getContentPane().add(jPanel2, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>

    /**
     * 重置信息
     * @param evt
     */
    private void btnResetMouseClicked(java.awt.event.MouseEvent evt) {
        // TODO add your handling code here:
        txtUserRename.setText(friend.getFriendRemark());
    }

    /**
     * 修改信息
     */
    private void btnEditMouseClicked(java.awt.event.MouseEvent evt) {
        // TODO add your handling code here:
        int res = JOptionPane.showConfirmDialog(null, "是否修改信息", "是否继续", JOptionPane.YES_NO_OPTION);
        if (res == JOptionPane.YES_OPTION) {
            // 点击“是”后执行这个代码块
            //提交
            String friendId = txtUserId.getText();
            String FriendRemark = txtUserRename.getText();
            //提交给服务器
            InetAddress remoteAddr = null;
            try {
                remoteAddr = InetAddress.getByName(Consts.ipv4);
                DatagramSocket clientSocket = null;
                clientSocket = new DatagramSocket();

                Protocol protocol = new Protocol();
                protocol.setKey("EditRename");
                protocol.setToken(UserController.getToken());

                FriendsEntity friendsEntity = new FriendsEntity();
                friendsEntity.setUserId(UserController.getUserEntity().getId());
                friendsEntity.setFriendId(Long.valueOf(friendId));
                friendsEntity.setFriendRemark(FriendRemark);

                protocol.setData(JSON.toJSONString(friendsEntity));
                data = Translate.ObjectToByte(protocol);

                DatagramPacket packet = new DatagramPacket(data,data.length,remoteAddr,Consts.port);
                clientSocket.send(packet);
                L.OK(packet,Friends.class);

                data = new byte[8096];
                packet = new DatagramPacket(data,data.length,remoteAddr,Consts.port);
                clientSocket.receive(packet);

                L.OK(packet,Friends.class);

                Protocol pro = new Protocol();
                pro = (Protocol) Translate.ByteToObject(packet.getData());
                pro.key();


            } catch (IOException e) {
                e.printStackTrace();
            }
            //提交失败
        } else {
            return;
        }
    }

    // Variables declaration - do not modify
    private javax.swing.JButton btnEdit;
    private javax.swing.JButton btnReset;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea txtStyle;
    private javax.swing.JTextField txtUserId;
    private javax.swing.JTextField txtUserRename;
    private javax.swing.JTextField txtUsername;
    private FriendsEntity friend;
    private byte[] data = new byte[8096];
    // End of variables declaration
}
